﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.controllers;

public partial class Administration_ChangeYourCompany : BaseView, IChangeYourCompany
{
    ChangeYourCompanyController _controller;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ChangeYourCompanyController(this);
        SessionData sessionData = new SessionData();
        _controller.InitView(IsPostBack, sessionData.CurrentUser.UserID);
        if(!Page.IsPostBack)
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ChangeYourCompany);
    }


    #region IChangeYourCompany Members

    public List<CompanyInformation> AllowedCompanies
    {
        get
        {
           return GetAllowedCompanies();
        }
        set
        {
            PopulateAllowedCompanies(value);
        }
    }

    List<CompanyInformation> GetAllowedCompanies()
    {
        List<CompanyInformation> companies = new List<CompanyInformation>();
        foreach (ListItem item in ddlYourCompanies.Items)
        {
            int currentitemid = int.Parse(item.Value);
            CompanyInformation company = new CompanyInformation() { CompanyID = currentitemid };
            companies.Add(company);
        }
        return companies;
    }
    private void PopulateAllowedCompanies(List<CompanyInformation> companies)
    {
        ddlYourCompanies.Items.Clear();
        foreach (var company in companies)
        {
            ListItem item = new ListItem();
            item.Text = company.CompanyName;
            item.Value = company.CompanyID.ToString();
            ddlYourCompanies.Items.Add(item);
        }
    }


    public int SelectCompanyID
    {
        get
        {
            SessionData sessiondata = new SessionData();
            return sessiondata.CurrentCompany.CompanyID;
        }
        set
        {
            SessionData sessiondata = new SessionData();
            sessiondata.CurrentCompany = _controller.GetCompanyInformation(value);
            ddlYourCompanies.SelectedValue = value.ToString();
        }
    }

    #endregion
    
    protected void btnChangeCompany_Click(object sender, EventArgs e)
    {
        SelectCompanyID = int.Parse(ddlYourCompanies.SelectedValue);
        NavigationController.OpenView("Administration");
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        NavigationController.OpenView("Administration");
    }
}